package com.sweetline.pub.dao;

import java.sql.SQLException;
import java.util.List;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.sweetline.pub.exception.AppException;

/**
 * 
 *Titile:DAO基类<br>
 *Description:<br> 
 *Date: Mar 8, 2013<br> 
 *Copyright (c) 2013 SWEET-LINE<br> 
 *
 *Author xiacj
 */
public class BaseDao  implements IBaseDao{

	 private SqlMapClient sqlMapClient;

	public SqlMapClient getSqlMapClient() {
		return sqlMapClient;
	}

	public void setSqlMapClient(SqlMapClient sqlMapClient) {
		this.sqlMapClient = sqlMapClient;
	}
	
	
	
	/**
	 * 查询方法
	 * 
	 * @param qryKey
	 * @param param
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T1,T2> T1 queryForObject(String qryKey,T2 param){
		T1 obj = null;
		try {
			obj = (T1)sqlMapClient.queryForObject(qryKey, param);
		} catch (SQLException e) {
			throw new AppException(e.getErrorCode()+"",e.getMessage());
		}
		return obj;
		
	}
	
	/**
	 * 查询列表方法
	 * 
	 * @param <T1>
	 * @param <T2>
	 * @param qryKey
	 * @param param
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T1,T2> List<T1> queryForList(String qryKey,T2 param){
		List<T1> list = null;
		try {
			list = sqlMapClient.queryForList(qryKey,param);
		} catch (SQLException e) {
			throw new AppException(e.getErrorCode()+"",e.getMessage());
		}
		return list;
	}
	
	public void insert(String addKey,Object param){
		try {
			sqlMapClient.insert(addKey, param);
			
		} catch (SQLException e) {
			throw new AppException(e.getErrorCode()+"",e.getMessage());
		}
	}
	
	public void delete(String delKey,Object param){
		try {
			sqlMapClient.delete(delKey, param);
		} catch (SQLException e) {
			throw new AppException(e.getErrorCode()+"",e.getMessage());
		}
	}
	
	public void update(String updateKey,Object param){
		try {
			sqlMapClient.update(updateKey, param);
		} catch (SQLException e) {
			throw new AppException(e.getErrorCode()+"",e.getMessage());
		}
	}
	
	public String getSeq(String seqName){
		try {
			return (String)sqlMapClient.queryForObject("getSeqBySeqName", seqName);
		} catch (SQLException e) {
			throw new AppException(e.getErrorCode()+"",e.getMessage());
		}
	}
	
	
}
